import apis from '../../http/apis/apis';
export default {
    namespaced: true,
    state: {
        ordersData: [],
        searchData: {
            searchBy: 'startTime',
            searchValue: '',
            currentPage: 1,
            pageSize: 2
        },
        totalPages: '',
        total: 0,


        //历史订单

        historyOrders: {
            data: [],
            currentPage: 1,
            totalPage: 1,
            totalNum: 0
        },
        pageSize: 3,
        searchHistoryData: {
            options: 'startTime',
            search_data: '',
        },
    },
    mutations: {
        updateOrdersData(state, data) {
            state.ordersData = data;
        },
        changeSearchData(state, data) {
            state.searchData.searchBy = data.searchBy;
            if (state.searchData.searchBy == 'startTime') {
                state.searchData.searchValue = data.date; //按下单日期查找
            } else {
                state.searchData.searchValue = data.searchValue;
            }

        },
        getOrders(state, data) {
            state.pages = data;

        },
        changePageSize(state, data) {
            state.ordersData.pageSize = data;
        },
        changeCurrentPage(state, data) {
            state.searchData.currentPage = data;
        },
        getTotalPages(state, data) {
            state.totalPages = data;
        },
        //----------------
        getPages(state, pages) {
            state.pages = pages;
        },
        changePageSize(state, pageSize) {
            state.searchData.pageSize = pageSize;
        },
        getTotal(state, total) {
            state.total = total;
        },
        changeHitoryOrders(state, val) {

            // console.log(arr);
            state.historyOrders = { ...val };
        },

        //改变历史订单当前页
        //分页，上下页
        changCurrentPage(state, val) {
            state.historyOrders.currentPage = val - 0;
        },
        //改变每页显示多少条数据
        changePageSize(state, val) {
            state.historyOrders.currentPage = 1;
            state.pageSize = val;
        },
        //改变搜索类型和搜索数据
        searchHistory(state, val) {
            state.searchHistoryData = { ...val };
        }
    },
    actions: {
        async getOrders(context) {
            const msg = await apis.orders.getOrders({
                ...context.state.searchData
            });

            let pages = Math.ceil(msg.data.total / context.state.searchData.pageSize);
            context.commit('getTotalPages', pages);
            context.commit('updateOrdersData', msg.data.data);
            context.commit('getTotal', msg.data.total);
            return msg
        },
        async deleteOrders(context, id) {
            const msg = await apis.orders.deleteOrders({
                _id: id
            });

            return msg;
        },
        async modifyOrders(context, order) {
            const {
                _id,
                state,
            } = order;
            const msg = await apis.orders.modifyOrders({
                _id,
                state
            });
            return msg;
        },
        //获取历史订单
        async getHistoryOrders(context, val) {
            const { data } = await apis.orders.getHistoryOrders({
                ...context.state.searchHistoryData,
                currentPage: context.state.historyOrders.currentPage,
                pageSize: context.state.pageSize,
            });
            console.log('获取历史----------------', data);
            context.commit('changeHitoryOrders', data.msg);
        },



        //添加评价
        async addEvaluate(context, val) {
            console.log(val);
            context.dispatch('modifyCredits', val);
            const { data } = await apis.orders.addEvaluate({
                ...val
            })
            return data
        },


        //根据评价修改租客信用值modifyCredits
        async modifyCredits(context, val) {

            const { data } = await apis.tenants.modifyCredits({
                ...val
            })
            return data
        },



        //删除历史订单
        async deleteHistoryOrders(context, val) {

            const { data } = await apis.orders.deleteOrders({
                _id: val
            });

            return data;
        },



    }
}